package site.marqstree.kotlin.usercenter.ui.fragment.me

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_me.*
import org.jetbrains.anko.support.v4.startActivity
import site.marqstree.kotlin.library.base.ui.fragment.BaseFragment
import site.marqstree.kotlin.library.base.util.GlideUtil
import site.marqstree.kotlin.library.base.util.UserUtil
import site.marqstree.kotlin.usercenter.R
import site.marqstree.kotlin.usercenter.ui.activity.login.LoginActivity
import site.marqstree.kotlin.usercenter.ui.activity.userinfo.UserInfoActivity


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.ui.fragment.me
 * 文件名: MeFragment
 * 创建者: marqstree
 * 创建时间: 2020/3/4 9:50
 * 描述: TODO
 */
class MeFragment :BaseFragment(), View.OnClickListener {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val rootView = inflater.inflate(R.layout.fragment_me,null)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        loadData()
    }

    /*
        初始化视图
     */
    private fun initView() {
        mUserIconIv.setOnClickListener(this)
        mUserNameTv.setOnClickListener(this)

        mWaitPayOrderTv.setOnClickListener(this)
        mWaitConfirmOrderTv.setOnClickListener(this)
        mCompleteOrderTv.setOnClickListener(this)
        mAllOrderTv.setOnClickListener(this)
        mAddressTv.setOnClickListener(this)
        mShareTv.setOnClickListener(this)
        mSettingTv.setOnClickListener(this)
    }


    /*
        加载初始数据
     */
    private fun loadData() {
        if (UserUtil.isLogined()) {
            val userIcon = UserUtil.getUserIcon()
            if (userIcon.isNotEmpty()) {
                GlideUtil.loadImage(context!!,userIcon,mUserIconIv)
            }else{
                mUserIconIv.setImageResource(R.drawable.icon_default_user)
            }
            mUserNameTv.text = UserUtil.getUserName()
        } else {
            mUserIconIv.setImageResource(R.drawable.icon_default_user)
            mUserNameTv.text = getString(R.string.un_login_text)
        }
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.mUserIconIv,R.id.mUserNameEt->{
                if(UserUtil.isLogined())
                    startActivity<UserInfoActivity>()
                else
                    startActivity<LoginActivity>()
            }
        }
    }
}